package com.joinway.extension.socket.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FrameDataHandler<T> extends IoHandlerAdapter {

	private final static Logger log = LoggerFactory.getLogger(FrameDataHandler.class);
	
	protected IMessageProcessor<T> processor;
	
	public FrameDataHandler(IMessageProcessor<T> processor){
		this.processor = processor;
	}
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		log.error("session " + session.getId() + " got exception ", cause);
		processor.onError(session, cause);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void messageReceived(IoSession session, Object frame) throws Exception {
		log.info("session {} received frame", session.getId());
		processor.onReceived(session, (T)frame);
//		log.debug("message type {}", message.getClass().getCanonicalName());
//		log.debug("message is {}", message);
	}

}
